<template>
  <main class="list">
    <section class="list-con">
      <h2>{{ title }}</h2>
      <div class="list-con-items">
        <div class="list-item" v-for="item in goodsList" :key="item.id">
          <router-link
            :to="{
              path: '/details',
              query: {
                id: item.id,
              },
            }"
          >
            <img :src="host + item.image" alt="" title="点击进入详情页" />
          </router-link>
          <h4>{{ item.name }}</h4>
          <div>
            <p>￥{{ item.price }}</p>
            <button @click="addToCart(item.id)">加入购物车</button>
          </div>
        </div>
        <div v-for="(item, index) in insufficient" :key="index"></div>
      </div>
      <!-- 新增代码 -->
      <div class="paging">
        <ul>
          <li>
            <button @click="previousPage" v-show="page !== 1">上一页</button>
          </li>
          <li>第{{ page }}页/共{{ maxPage }}页</li>
          <li>
            <button @click="nextPage" v-show="page !== maxPage">下一页</button>
          </li>
        </ul>
      </div>
    </section>
  </main>
</template>

<script>
import { mapState } from "vuex";
import config from "../../config";
import { addToCartApi } from "../../api";
export default {
  name: "List",
  data() {
    return {
      host: config.host,
      // 当前页数
      page: 1,
      // 商品不够空 div 补位 支撑结构
      insufficient: 0,
    };
  },
  computed: {
    kind() {
      return this.$route.params.kind;
    },
    keyword() {
      return this.$route.query.keyword;
    },
    typeId() {
      return this.$route.query.typeId;
    },
    type() {
      return this.$route.query.type;
    },
    //根据路由传参来计算路由title信息
    title() {
      if (this.kind) {
        return this.kind;
      }
      return `搜索"${this.keyword}"结果`;
    },
    ...mapState("goods", ["goodsList", "maxPage"]),
  },
  watch: {
    // 监听 商品列表数据来判断商品数量是否为 4的整数倍 不是 修改要补位数
    goodsList: {
      immediate: true,
      deep: true,
      handler(newValue) {
        // 判断是否为null
        if (!newValue) return;
        // 设置 this.insufficient 补位长度
        if (newValue.length % 4 === 0) {
          this.insufficient = 0;
        } else {
          this.insufficient = 4 - (newValue.length % 4);
        }
      },
    },

    // 监听 title 的变化 根据 type 来更新指定数据
    title: {
      immediate: true,
      deep: true,
      handler() {
        this.$store.commit("goods/DELETE");
        this.page = 1;
        if (this.type == 1) {
          this.$store.dispatch("goods/getList", { page: this.page });
        } else if (this.type == 2) {
          this.$store.dispatch("goods/getByType", {
            type_id: this.typeId,
            page: this.page,
          });
        } else if (this.type == 3) {
          this.$store.dispatch("goods/getByName", {
            page: this.page,
            name: this.keyword,
          });
        } else if (this.type == 4) {
          this.$store.dispatch("goods/getByHot");
        } else if (this.type == 5) {
          this.$store.dispatch("goods/getByTime");
        }
      },
    },
    // 监视页数，当页数发生变化时更新数据
    page() {
      if (this.type == 1) {
        this.$store.dispatch("goods/getList", { page: this.page });
      } else if (this.type == 2) {
        this.$store.dispatch("goods/getByType", {
          type_id: this.typeId,
          page: this.page,
        });
      } else if (this.type == 3) {
        this.$store.dispatch("goods/getByName", {
          page: this.page,
          name: this.keyword,
        });
      } else if (this.type == 4) {
        this.$store.dispatch("goods/getByHot");
      } else if (this.type == 5) {
        this.$store.dispatch("goods/getByTime");
      }
    },
  },
  methods: {
    // 上一页
    previousPage() {
      if (this.page <= 1) return;
      this.page--;
    },
    // 下一页
    nextPage() {
      if (this.page >= this.maxPage) return;
      this.page++;
    },
    // 加入购物车
    addToCart(goods_id) {
      addToCartApi(
        (response) => {
          if (response.data.status === 1) {
            this.$bus.$emit("showTips", "添加购物车成功！");
            // 重新获取购物车长度
            this.$store.dispatch("user/getCartLen");
            // 提示信息
          }
        },
        (err) => {
          console.log(err);
        },
        { goods_id, amount: 1 }
      );
    },
  },
  mounted() {},
};
</script>

<style lang="stylus" scoped>
.list {
  width: 100%;
  min-width: 800px;
  min-height: 400px;
  margin-top: 60px;
  background-color: #f8f8f8;

  .list-con {
    width: 80%;
    min-width: 800px;
    margin: 0 auto;
    padding: 20px 0 30px;

    h2 {
      height: 60px;
      font-size: 24px;
      color: #fe4d1b;
      line-height: 30px;
    }
  }

  .list-con-items {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;

    &>div {
      width: 22%;
    }

    .list-item {
      background-color: #fff;
      padding-bottom: 6px;
      margin-bottom: 20px;

      a {
        text-decoration: none;
        color: #999;
      }

      img {
        width: 100%;
      }

      h4 {
        margin-left: 6px;
        font-size: 14px;
        line-height: 24px;
      }

      div {
        display: flex;
        justify-content: space-between;
        padding: 0 6px;
      }

      p {
        font-size: 12px;
        color: #fe4d1b;
      }

      button {
        padding: 4px 6px;
        border: none;
        border-radius: 4px;
        margin-right: 6px;
        background-color: #fe4d1b;
        color: #fff;
        cursor: pointer;
      }

      &:hover {
        outline: 2px solid #fe4d1b;
      }
    }
  }

  .paging {
    font-size: 16px;

    ul {
      display: flex;
      justify-content: center;
    }

    li {
      width: 100px;
      text-align: center;
    }

    li:nth-of-type(2) {
      line-height: 28px;
    }

    button {
      height: 26px;
      font-size: 14px;
      padding: 0 20px;
      border: none;
      border-radius: 4px;
      background-color: #fe4d1b;
      color: #fff;
      cursor: pointer;
    }
  }
}
</style>